package com.gdufs.secondhandwebsite.service.Impl;

import com.gdufs.secondhandwebsite.dao.GoodsMapper;
import com.gdufs.secondhandwebsite.entity.Goods;
import com.gdufs.secondhandwebsite.service.GoodsService;
import com.gdufs.secondhandwebsite.service.ex.UserNotFoundException;
import com.gdufs.secondhandwebsite.service.ex.UserNotLoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
public class GoodsServiceImp implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    public Goods  getGoodsById(Long goodsId){
      return  goodsMapper.SelectGoodsByPrimaryKey(goodsId);
    }

   public void insertGoods(Goods goods, String createUser,String userImg){

       if (createUser == null) {
           // 是：抛出UserNotFoundException异常
           throw new UserNotLoginException("用户尚未登录");
       }
        goods.setCreateUser(createUser);
        goods.setUserImg(userImg);
        goodsMapper.CreateGoods(goods);
    }


   public boolean updateGoods(Goods goods){
        int result = goodsMapper.UpdateGoodsSelective(goods);
        if(result<=0){
            return false;
        }
        else{
            return true;
        }
    }

    public boolean removeGoods(Long goodsId){

        int result = goodsMapper.UpdateGoodsByPrimaryKey(goodsId,true);
        if(result<=0){
            return false;
        }
        else{
            return true;
        }
    }

    public List<Goods> getAllGoods(){
      return   goodsMapper.getAllGoods();
    }
    public List<Goods> SelectByGoodsType(String tagName){
        return  goodsMapper.SelectByGoodsType(tagName);
    }
}
